
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.entity.AccountSecurityInfo;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;
import com.kugou.game.sdk.utils.RSAUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:设置支付密码
 *
 * @author jerryliu
 * @since 2015年8月25日 上午10:08:50
 */
public class SetPayPsdTask {

    public SetPayPsdResponse request(String username, String password, String paypassword) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(username);
        SetPayPsdResponse respObj = new SetPayPsdResponse();
        ResponsePackage resp = new SetPayPsdResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();

        params.put("cmd", ProtocolCmd.CMD_SET_PAY_PASSWD);
        params.put("username", username);
        params.put("password", RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(password, "utf-8")));
        params.put("paypwd", RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(paypassword, "utf-8")));
        requestParams.add(params);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    static class SetPayPsdResponsePackage extends HttpBaseResponsePackage<SetPayPsdResponse> {

        @Override
        protected void handleResponse(SetPayPsdResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "SetPayPsdTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);

                    if (response.isUseful() && ProtocolCmd.CMD_SET_PAY_PASSWD.equals(cmd)) {
                        JSONObject data = jsonResp.optJSONObject("data");

                        AccountSecurityInfo info = new AccountSecurityInfo();
                        if (!data.isNull("safety_score")) {
                            info.setSecurityScore(data.getInt("safety_score"));
                        }
                        if (!data.isNull("login_email")) {
                            info.setLoginEmail(data.getString("login_email"));
                        }
                        if (!data.isNull("security_email")) {
                            info.setSecurityEmail(data.getString("security_email"));
                        }
                        if (!data.isNull("question_id")) {// 未设置安全问题时返回255
                            info.setSecurityQuestionId(data.getInt("question_id"));
                        }
                        if (!data.isNull("is_setidcard")) {
                            info.setSetIdentity(data.getInt("is_setidcard") == 1);
                        }
                        if (!data.isNull("is_setpaypwd")) {
                            info.setPayPsd(data.getInt("is_setpaypwd"));
                        }
                        response.setInfo(info);
                        response.setOk(true);
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    public static class SetPayPsdResponse extends ResponseMessage {
        private static final long serialVersionUID = -4820735216365527465L;

        private AccountSecurityInfo info;

        public AccountSecurityInfo getInfo() {
            return info;
        }

        public void setInfo(AccountSecurityInfo info) {
            this.info = info;
        }
    }
}
